﻿using HuaCheng.CommonClass;
using HuaCheng.Controller;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    public partial class AutoConnect : Form
    {
        List<string> changedIPs = null;
        public delegate void DeletePageInfo(List<string> changedIPs);//委托
        public event DeletePageInfo IpPageChanged;//删除ip对应的原页面信息事件
        public AutoConnect(List<string> ips)
        {
            //allDevChl = alldev;
            changedIPs = ips;
            InitializeComponent();           
        }
        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="changedIPs"></param>
        public void OnIpChanged(List<string> changedIPs)
        {
            if (IpPageChanged != null)
            {
                IpPageChanged(changedIPs);
            }
        }
        //
        /// <summary>
        /// 发送广播包
        /// </summary>
        private void Broadcast()
        {
            if (IndexPage.language == "en-US")
            {
                this.tbMsg.AppendText("\r\n" + " Broadcasting...");
            }
            else
            {
                this.tbMsg.AppendText("\r\n" + "广播中...");
            }
            AutoConControl.BeginBroadcast();
            if (IndexPage.language == "en-US")
            {
                this.tbMsg.AppendText("\r\n" + "Broadcast complete。");
            }
            else
            {
            this.tbMsg.AppendText("\r\n" + "广播包发送完毕。");
            }
        }
        /// <summary>
        /// 接收反馈包并更新设备信息
        /// </summary>
        private void UpdateDeviceInfo()
        {
            if (IndexPage.language == "en-US")
            {
                this.tbMsg.AppendText("\r\n" + "Receiving bags...");
                //AutoConControl.BeginReceive();
                this.tbMsg.AppendText("\r\n" + "Device ip update。");
                this.tbMsg.AppendText("\r\n" + "Self-organized network complete");
            }
            else
            {
                this.tbMsg.AppendText("\r\n" + "接收反馈包中...");
                //AutoConControl.BeginReceive();
                this.tbMsg.AppendText("\r\n" + "设备ip更新完毕。");
                this.tbMsg.AppendText("\r\n" + "自动组网完成");
            }
        }
        /// <summary>
        /// 开始按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            //this.tbMsg.Clear();
            Broadcast();
            UpdateDeviceInfo();
           //触发委托把通道页面带回去
            OnIpChanged(changedIPs);
        }
        #region 国际化
        private void AutoConnect_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("selforganizednetwork");
            this.btnStart.Text = ResourceCulture.GetString("Start");
        }
        #endregion
    }
}
